<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('checkboxes');

class JFormFieldListTables extends JFormFieldCheckboxes
{

    public $type = 'ListTables';

    protected function getOptions()
    {
        $aOptions = array();

        $oConfig = JFactory::getConfig();

        $oDatabase = JFactory::getDbo();

        // Get table in database.
        $aTables = $oDatabase->getTableList();
        
        // Get the tables of component.
        $oApplication = JFactory::getApplication();
        
        $oQuery = $oDatabase->getQuery(true);

        $oQuery->select('at.sql_table_name');
        $oQuery->from('#__auto_table AS at');
        $oQuery->where('at.component_id = ' . (int) $oApplication->input->getInt('iComponentId'));

        $oDatabase->setQuery($oQuery);

        try
        {
            $aExistTables = $oDatabase->loadObjectList();
        }
        catch (RuntimeException $oException)
        {
            JError::raiseWarning(500, $oException->getMessage());
        }
        
        // Check table exist and add to options.
        foreach ($aTables as $strTable)
        {
            $bIsExist = false;
            
            foreach($aExistTables as $oTable)
            {
                if ($oTable->sql_table_name == $strTable)
                {
                    $bIsExist = true;
                    
                    break;
                }
            }
            
            if ($bIsExist == false)
            {
                $oOption = new stdClass;

                $oOption->text = $strTable;
                $oOption->value = $strTable;

                $aOptions[] = $oOption;
            }
        }

        return $aOptions;
    }

    protected function getInput()
    {
        $aHtml = array();

        $sClass = $this->element['class'] ? ' class="checkboxes ' . (string) $this->element['class'] . '"' : ' class="checkboxes"';

        $aCheckedOptions = explode(',', (string) $this->element['checked']);

        $aHtml[] = '<a href="javascript:void(0);" class="btn check-all" onclick="Autogenerator.Import.checkAll();">' . JText::_('COM_AUTOGENERATOR_IMPORT_FIELD_TABLES_CHECKBOX_TOGGLE_LABEL') . '</a>';
        $aHtml[] = '&nbsp;';
        $aHtml[] = '<a href="javascript:void(0);" class="btn check-all" onclick="Autogenerator.Import.clearAll();">' . JText::_('COM_AUTOGENERATOR_IMPORT_FIELD_TABLES_CHECKBOX_CLEAR_LABEL') . '</a>';
        
        // Start the checkbox field output.
        $aHtml[] = '<fieldset id="' . $this->id . '"' . $sClass . '>';

        // Get the field options.
        $aOptions = $this->getOptions();
        
        
        
        // Build the checkbox field output.
        $aHtml[] = '<ul>';

        foreach ($aOptions as $iKey => $oOption)
        {
            // Initialize some option attributes.
            if (!isset($this->value) || empty($this->value))
            {
                $sChecked = (in_array((string) $oOption->value, (array) $aCheckedOptions) ? ' checked="checked"' : '');
            }
            else
            {
                $sValue = !is_array($this->value) ? explode(',', $this->value) : $this->value;

                $sChecked = (in_array((string) $oOption->value, $sValue) ? ' checked="checked"' : '');
            }

            $sClass = !empty($oOption->class) ? ' class="' . $oOption->class . '"' : '';

            $sDisabled = !empty($oOption->disable) ? ' disabled="disabled"' : '';

            // Initialize some JavaScript option attributes.
            $sOnclick = !empty($oOption->onclick) ? ' onclick="' . $oOption->onclick . '"' : '';

            $aHtml[] = '<li>';

            $aHtml[] = '<input type="checkbox" id="' . $this->id . $iKey . '" name="' . $this->name . '"' . ' value="' . htmlspecialchars($oOption->value, ENT_COMPAT, 'UTF-8') . '"' . $sChecked . $sClass . $sOnclick . $sDisabled . '/>';

            $aHtml[] = '<label for="' . $this->id . $iKey . '"' . $sClass . '>' . JText::_($oOption->text) . '</label>';
            
            $aHtml[] = '</li>';
        }

        $aHtml[] = '</ul>';

        // End the checkbox field output.
        $aHtml[] = '</fieldset>';

        return implode($aHtml);
    }

}
